package com.springboot.cli.controller;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.google.common.collect.Lists;
import com.springboot.cli.domain.ShiftEntity;
import com.springboot.cli.domain.Word;
import com.springboot.cli.service.IShiftService;
import com.springboot.cli.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;

/**
 * description:
 *
 * @author huxingjie
 * @date 2023/11/7 10:11
 */
@RestController
public class ScrewController {
    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    IShiftService shiftService;

    @Autowired
    private WordService wordService;

    @PostMapping("shift")
    public List<ShiftEntity> getShifts(@RequestBody ShiftEntity shiftEntity) {
        return shiftService.list(shiftEntity);
    }

    @GetMapping("word")
    public List<Word> word() throws Exception {
        return wordService.generateWord();
    }

    /**
     * 树形递归查询地区信息
     */
    @GetMapping("screw")
    public void screw() {
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件路径，自己mac本地的地址，这里需要自己更换下路径
                .fileOutputDir("d:\\screw\\")
                // 打开目录
                .openOutputDir(true)
                // 文件类型
                .fileType(EngineFileType.WORD)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker).build();
        // 生成文档配置（包含以下自定义版本号、描述等配置连接）
        Configuration config = Configuration.builder()
                .version("1.0.1")
                .description("生成文档信息描述")
                .dataSource(dataSourceMysql)
                .engineConfig(engineConfig)
                .produceConfig(getProcessConfig())
                .build();
        // 执行生成
        new DocumentationExecute(config).execute();
    }

    /**
     * 配置想要生成的表+ 配置想要忽略的表
     *
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig() {
        List<String> designedTablePrefix = Arrays.asList("ICIS_");
        return ProcessConfig.builder()
                .designatedTablePrefix(Lists.newArrayList(designedTablePrefix))
                .build();
    }
}
